﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Application.MainModule.BranchesManagement;
using Domain.MainModule.Entities;
using Presentation.Web.MVC.Client.ViewModels;

namespace Presentation.Web.MVC.Client.Controllers
{
    public class BranchController : Controller
    {
        #region private member

        /// <summary>
        /// Instance of the branch service.
        /// </summary>
        private IBranchManagementService _branchService;

        #endregion

        #region constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="BranchController"/> class.
        /// </summary>
        /// <param name="branchService">The branch service.</param>
        public BranchController(IBranchManagementService branchService)
        {
            _branchService = branchService;
        }

        #endregion

        #region public methods

        /// <summary>
        /// Display a paged listing of the existing branchs.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">The page size.</param>
        /// <returns></returns>
        public ActionResult Index(int page, int pageSize)
        {
            IList<Branch> branchs = _branchService.FindPagedBranches(page, pageSize);
            return View(new BranchListViewModel(branchs, page, pageSize));
        }

        /// <summary>
        /// Shows a specific branch in detail.
        /// </summary>
        /// <param name="branchId">The branch id.</param>
        /// <returns>A view of the branch details.</returns>
        public ActionResult Details(Guid branchId)
        {
            //Get the branch aggregate.
            Branch branch = _branchService.FindBranchByBranchId(branchId);
            //Display its details.
            return View(branch);
        }

        /// <summary>
        /// Displays a form to create a new branch.
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// Creates the specified branch based on the request data and displays it.
        /// </summary>
        /// <param name="customer">The branch to add.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(Branch branch)
        {
            //Check if there aren't validation errors on the customer.
            if (ModelState.IsValid)
            {
                //Add branch to the data base.
                _branchService.AddBranch(branch);
                //Show the first page of the branch.
                return RedirectToAction("Index", new { page = 0, pageSize = 10 });
            }
            else
            {
                //Return the Create view and display the validation errors.
                return View();
            }
        }

        /// <summary>
        /// Displays a form to edit the branch.
        /// </summary>
        /// <param name="branchId">The branch code.</param>
        /// <returns></returns>
        public ActionResult Edit(Guid branchId)
        {
            //Get the branch aggregate to edit.
            Branch branch = _branchService.FindBranchByBranchId(branchId);
            //Return the
            return View(branch);
        }

        /// <summary>
        /// Saves the changes on the editions performed to the specified branch.
        /// </summary>
        /// <param name="branch">The branch.</param>
        /// <returns>The details of the branch.</returns>
        [HttpPost]
        public ActionResult Edit(Branch branch)
        {
            if (ModelState.IsValid)
            {
                _branchService.ChangeBranch(branch);
                return RedirectToAction("Details", new RouteValueDictionary() { { "branchId", branch.BranchId } });
            }
            else
            {
                return View();
            }
        }

        /// <summary>
        /// Deletes the specified branch.
        /// </summary>
        /// <param name="branchId">The branch code.</param>
        /// <returns>The details of the branch.</returns>
        public ActionResult Delete(Guid branchId)
        {
            Branch branch = _branchService.FindBranchByBranchId(branchId);
            _branchService.RemoveBranch(branch);
            
            return RedirectToAction("Index", new RouteValueDictionary() { { "page", 0 }, { "pageSize", 10 } });
        }

        #endregion
    }
}
